// 设计一个图书管理系统，其中包括Book类和Library类。
// Book类应该包括书名、作者和ISBN等属性，并且能够实现借出和归还书籍的方法。
// Library类应该包括一个可用书籍列表和一个已借出书籍列表，以及能够添加新书、借出书籍和归还书籍的方法。
// （apply、unapply方法必须使用）

import scala.io.StdIn

object LibrarySystem {
    class Book(val title: String, val author: String, val ISBN: String) {
        def borrowBook(): Unit = {
            println(s"You have borrowed the book '$title' successfully!")
        }

        def returnBook(): Unit = {
            println(s"You have returned the book '$title' successfully!")
        }
    }

    object Book {
        def apply(title: String, author: String, ISBN: String): Book = new Book(title, author, ISBN)

        def unapply(book: Book): Option[(String, String, String)] = Some((book.title, book.author, book.ISBN))
    }

    class Library(var books: List[Book], var borrowedBooks: List[Book]) {
        def addBook(bookTitle: String, author: String, ISBN: String): Unit = {
            val newBook = Book(bookTitle, author, ISBN)
            books = newBook :: books
            println(s"You have added the book '$bookTitle' successfully!")
        }

        def borrowBook(bookTitle: String): Unit = {
            val bookToBorrow = books.find(_.title == bookTitle)
            bookToBorrow match {
                case Some(book) =>
                    books = books.filterNot(_ == book)
                    borrowedBooks = book :: borrowedBooks
                    book.borrowBook()
                case None =>
                    println(s"The book '$bookTitle' is not available in the library.")
            }
        }

        def returnBook(bookTitle: String): Unit = {
            val bookToReturn = borrowedBooks.find(_.title == bookTitle)
            bookToReturn match {
                case Some(book) =>
                    borrowedBooks = borrowedBooks.filterNot(_ == book)
                    books = book :: books
                    book.returnBook()
                case None =>
                    println(s"You have not borrowed the book '$bookTitle'.")
            }
        }
    }
}

